<?php
class Model_Product_Search
{
    protected $_params;
    protected $_post;
    protected $_select;
    protected $_result;
    protected $_logic = 'or';
    
    
    public function __construct(array $post)
    {
        $paramsNamespace = new Zend_Session_Namespace('Params');
        if(count($post)){
            $paramsNamespace->search = $this->_post = $post;
        } else {
            $this->_post = $paramsNamespace->search;
        }
        $this->_postToParams();
        $this->_getSelect();
    }
    
    public function __get($field)
    {
        return $this->_{$field};   
    }
    
    protected function _postToParams()
    { 
        foreach($this->_post as $key=>$value){
           $type = explode('_', $key);
           $this->_params[$type[0]][] = $value;
        }
    }
    
    protected function _getSelect()
    {
        $mapper = new Model_Product_Mapper();
        $select = $mapper->getTable()->select();
        if(key_exists('cat', $this->_params)){
            foreach($this->_params['cat'] as $value){
                if($this->_logic == 'or'){
                    $select->orWhere('category_id  = ' . (int) $value);
                } else {
                    $select->Where('category_id  = ' . (int) $value);
                }
            }
        }
        if(key_exists('class', $this->_params)){
            foreach($this->_params['class'] as $value){
                  if($this->_logic == 'or'){
                        $select->orWhere("category  =  '$value'");
                    } else {
                        $select->Where("category  =  '$value'");
                    }
            }
        }
        if(key_exists('gender', $this->_params)){
            foreach($this->_params['gender'] as $value){
                    if($this->_logic == 'or'){
                        $select->orWhere("gender  = '$value'");
                    } else {
                        $select->Where("gender  =  '$value'");
                    }
            }
        }
        if(key_exists('price', $this->_params)){
            if(count($this->_params['price'])==2){
                    if($this->_logic == 'or'){
                        $select->orWhere("price between {$this->_params['price'][0]} and {$this->_params['price'][1]}");
                    } else {
                        $select->Where("price between {$this->_params['price'][0]} and {$this->_params['price'][1]}");
                    }
            }
        }
        if(key_exists('size', $this->_params)){
            if(count($this->_params['size'])==2){
                    if($this->_logic == 'or'){
                        $select->orWhere("size between {$this->_params['size'][0]} and {$this->_params['size'][1]}");
                    } else {
                        $select->Where("size between {$this->_params['size'][0]} and {$this->_params['size'][1]}");
                    }
            }
        }
        $this->_select = $select;
        return $this->_select;
    }
    
    public  function getResultByPage($page, $itemCountPerPage =30)
    {
            $mapper = new Model_Product_Mapper();
            $select = $this->_select;
            $paginator = new Zend_Paginator(new App_Paginator_Adapter_DbTableSelect($select, $mapper));
            $paginator->setItemCountPerPage($itemCountPerPage);
            $paginator->setCurrentPageNumber($page);
            $this->_result = $paginator;
            return $this->_result;
    }
}